package Hot100.DP;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class generate {
    public static void main(String[] args) {
        List<List<Integer>> list = new generate().generate(7);
        System.out.println(list);
    }
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result  = new ArrayList<>();
        if(numRows == 1){
            List list = new ArrayList<>();
            list.add(1);
            result.add(list);
            return result;
        }
        int[][] tri = new int[numRows + 1][numRows + 1];
        tri[1][1] = 1;
        tri[2][1] = 1;
        tri[2][2] = 1;
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        list1.add(1);
        list2.add(1);
        list2.add(1);
        result.add(list1);
        result.add(list2);
        int height = 3;
        while(height <= numRows){
            List<Integer> list = new ArrayList<>();
            for (int i = 1; i <= height; i++) {
                if(tri[height - 1][i] != 0 && tri[height - 1][i - 1] != 0){
                    tri[height][i] = tri[height - 1][i] + tri[height - 1][i - 1];
                    list.add(tri[height][i]);
                }else {
                    tri[height][i] = 1;
                    list.add(tri[height][i]);
                }
            }
            result.add(list);

            height++;
        }
        return result;
    }
}
